package com.wangxinenpu.springbootdemo.util;


import lombok.extern.slf4j.Slf4j;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;

@Slf4j
public class Generator {
	public static String generateKey() {
		KeyGenerator keyGenerator = null;
		try {
			keyGenerator = KeyGenerator.getInstance( "AES" );
		} catch ( NoSuchAlgorithmException e ) {
			throw new RuntimeException( "密钥生成失败" );
		}
		keyGenerator.init( 256 , new SecureRandom() );
		SecretKey secretKey = keyGenerator.generateKey();
		log.info( "密钥：{}" , secretKey.toString() );
		byte[] key = secretKey.getEncoded();
		log.info( "密钥：{}" , Base64.getEncoder().encodeToString( key ) );
		return Base64.getEncoder().encodeToString( key );
	}
}
